package com.huitone.gddw.controller.system;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huitone.gddw.dao.entity.SmLog;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.dto.system.SmLogDTO;
import com.huitone.gddw.service.system.ISmLogService;

/**
 * 门户日志 控制器
 * @author wuluodan
 *
 */
@Controller
@RequestMapping(value="smLog")
public class SmLogController {

	private final static Logger LOGGER = Logger.getLogger(SmLogController.class);
	
	@Autowired
	private ISmLogService smLogService;
	
	/**
	 * 查询
	 * @param page
	 * @param request
	 * @return QueryPage
	 */
	@ResponseBody
	@RequestMapping(value="query")
	public QueryPage<SmLogDTO> query(QueryPage<SmLog> page, HttpServletRequest request) {
		page.setParams(getParams(request));
		try {
			QueryPage<SmLogDTO> result = smLogService.selectPage(page);
			
			SmLog entity = new SmLog();
			entity.setLogType(SmLog.LOGTYPE_SELECT);
			entity.setContent("查询日志管理列表");
			entity.setResult(SmLog.RESULT_SUCCESS);
			smLogService.save(entity);
			
			return result;
		} catch (Exception e) {
			LOGGER.error("查询门户日志信息出现异常：" + e.getMessage(), e);
			
			SmLog entity = new SmLog();
			entity.setLogType(SmLog.LOGTYPE_SELECT);
			entity.setContent("查询日志管理列表");
			entity.setResult(SmLog.RESULT_FAIL);
			smLogService.save(entity);
			
			return null;
		}
	}
	
	/**
	 * 获取参数信息
	 * @param request
	 * @return Map<String, Object>
	 */
	private Map<String, Object> getParams(HttpServletRequest request) {
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		String logType = request.getParameter("logType");
		String userName = request.getParameter("userName");
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("logType", logType);
		params.put("userName", userName);
		return params;
	}
	
}
